package com.tmall.testgen.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tmall.testgen.client.commond.InfoReceiveListener;
import com.tmall.testgen.common.bo.BaseUserCenter;
import com.tmall.testgen.common.bo.interact.back.DBInfo;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo;

/**
 * @author haojia
 * @date 2014-07-04
 */
public class ClientDataCenter extends BaseUserCenter {

	private static ClientDataCenter instance=new ClientDataCenter();
	/**
	 * ip
	 */
	private String ip;
	/**
	 * pid
	 */
	private String pid;


	/**
	 * 测试名称
	 */
	private String testName;
	
	/**
	 * 生成的信息 projectName$src
	 */
	private String testProjectInfo;
	
	/**
	 * 选择的数据源
	 */
	private DBInfo dbInfo; 
	/**
	 * 选择的数据源
	 */
	
	private List<DBInfo> dbInfos;
	
	private int selectDbIndex;
	
	/**
	 * sql快照
	 */
	private String sqlSnap;
	
	
	/**
	 * 视图监听器
	 */
	private InfoReceiveListener listener;
	
	
	
	//==============仅应用于本地测试
	
	private Boolean isUseHsf=false;
	private String hsfConfig="";
	
	
	
	public static ClientDataCenter getInstance(){
		return instance;
	}
	public static void clear(){
		InfoReceiveListener listener=instance.getListener();
		instance=new  ClientDataCenter();
		instance.setListener(listener);
	}
	
	public void removeUserInfo(InterceptInfo info){
		super.getInfoes().remove(info);
	}
	public void addUserInfo(InterceptInfo info){
	    listener.addInterceptInfo(info);
	    
		super.addInterceptInfo(info);
		
		
	}
	
	public void addLoadedInterceptClassAndMethods(String className,String method){
	    super.addLoadedInterceptClassAndMethods(className, method);
	  
      listener.refreshTree();
	}
	public String clearSql(String sqlSnap){
		while(sqlSnap.indexOf("  ")>0){
			sqlSnap=sqlSnap.replaceAll("  ", " ");
		}
		return sqlSnap;
	}

	public  Map<String,String>getSqlSnapMap(String sqlSnap){
		
		sqlSnap=clearSql(sqlSnap.trim());
		Map<String,String> map=new HashMap<String,String>();
		
		if(sqlSnap==null||sqlSnap.length()==0)
			return map;
		sqlSnap=sqlSnap.toLowerCase();
		String[] sqls=sqlSnap.split(";");
		
		
		for(String sql:sqls){
			int begin=sql.indexOf(" from ")+6;
			int end=sql.indexOf(" ", begin);
			String tableName=sql.substring(begin, end);
			String newTaleName=tableName.replaceAll("`", "").replaceAll("'", "");
			sql=sql.replaceAll(tableName, newTaleName);
			map.put(newTaleName, sql);
		}
		return map;
		
	}



	public String getPid() {
		return pid;
	}



	public void setPid(String pid) {
		this.pid = pid;
	}



	public String getIp() {
		return ip;
	}



	public void setIp(String ip) {
		this.ip = ip;
	}





	public String getTestName() {
		return testName;
	}


	public void setTestName(String testName) {
		this.testName = testName;
	}


	public DBInfo getDbInfo() {
		return dbInfo;
	}


	public void setDbInfo(DBInfo dbInfo) {
		this.dbInfo = dbInfo;
	}


	public String getSqlSnap() {
		return sqlSnap;
	}


	public void setSqlSnap(String sqlSnap) {
		this.sqlSnap = sqlSnap;
	}


	public InfoReceiveListener getListener() {
		return listener;
	}


	public void setListener(InfoReceiveListener listener) {
		this.listener = listener;
	}

	public List<DBInfo> getDbInfos() {
		return dbInfos;
	}

	public void setDbInfos(List<DBInfo> dbInfos) {
		this.dbInfos = dbInfos;
	}

	public int getSelectDbIndex() {
		return selectDbIndex;
	}

	public void setSelectDbIndex(int selectDbIndex) {
		this.selectDbIndex = selectDbIndex;
	}
	public void setStart(boolean start) {
		super.setStart(start);
		if(listener!=null){
			listener.beginMonitor(start);
		}
		//listener.beginMonitor(start);
	}
	public Boolean getIsUseHsf() {
		return isUseHsf;
	}
	public void setIsUseHsf(Boolean isUseHsf) {
		this.isUseHsf = isUseHsf;
	}
	public String getHsfConfig() {
		return hsfConfig;
	}
	public void setHsfConfig(String hsfConfig) {
		this.hsfConfig = hsfConfig;
	}
    public String getTestProjectInfo() {
        return testProjectInfo;
    }
    public void setTestProjectInfo(String testProjectInfo) {
        this.testProjectInfo = testProjectInfo;
    }

	

}
